package com.example.hxpc.guidancemanagement.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.hxpc.guidancemanagement.R;
import com.example.hxpc.guidancemanagement.adapter.MyFrageStatePagerAdapter;
import com.example.hxpc.guidancemanagement.fragment.MainActFrament;
import com.example.hxpc.guidancemanagement.fragment.MainHonFrament;
import com.example.hxpc.guidancemanagement.fragment.MainPerFrament;
import com.example.hxpc.guidancemanagement.fragment.MainProFrament;

import java.util.ArrayList;

/**
 * Created by tao on 2017/11/26.
 * 主页面
 */

public class MainActivity extends FragmentActivity implements View.OnClickListener ,ViewPager.OnPageChangeListener{
    private ViewPager viewPager;
    private RelativeLayout rela_pro , rela_act , rela_hon , rela_per;
    private MainActFrament mainActFrament;
    private MainHonFrament mainHonFrament;
    private MainPerFrament mainPerFrament;
    private MainProFrament mainProFrament;
    private ArrayList<Fragment> mFramentList;
    private int currenttab = -1;
    private String type;

    private ImageView img_project ,img_activity , img_honor , img_person;
    private TextView te_project , te_activity , te_honor , te_person;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    public void initView(){
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        rela_pro = (RelativeLayout) findViewById(R.id.main_pro_rela);
        rela_act = (RelativeLayout) findViewById(R.id.main_act_rela);
        rela_hon = (RelativeLayout) findViewById(R.id.main_hon_rela);
        rela_per = (RelativeLayout) findViewById(R.id.main_per_rela);
        rela_pro.setOnClickListener(this);
        rela_act.setOnClickListener(this);
        rela_hon.setOnClickListener(this);
        rela_per.setOnClickListener(this);
        img_project = (ImageView) findViewById(R.id.project);
        img_activity = (ImageView) findViewById(R.id.activity);
        img_honor = (ImageView) findViewById(R.id.honor);
        img_person = (ImageView) findViewById(R.id.person);
        te_project = (TextView) findViewById(R.id.te_project);
        te_activity = (TextView) findViewById(R.id.te_activity);
        te_honor = (TextView) findViewById(R.id.te_honor);
        te_person = (TextView) findViewById(R.id.te_person);
        addFrament();
        viewPager.setAdapter(new MyFrageStatePagerAdapter(getSupportFragmentManager(),mFramentList));
        viewPager.setOnPageChangeListener(this);
        changeNavigation();
    }

    //导航随滑动切换
    public void changeNavigation(){
        if (viewPager.getCurrentItem()==currenttab){
            return;
        }
        currenttab = viewPager.getCurrentItem();
        if(currenttab == 0){
            changeColor(currenttab);
        }else if(currenttab == 1){
            changeColor(currenttab);
        }else if(currenttab == 2){
            changeColor(currenttab);
        }else if(currenttab == 3){
            changeColor(currenttab);
        }
    }

    //改变导航颜色
    public void changeColor(int current){
        if (current == 0){
            img_project.setImageResource(R.drawable.project_blue);
            img_activity.setImageResource(R.drawable.activity_gray);
            img_honor.setImageResource(R.drawable.honor_gray);
            img_person.setImageResource(R.drawable.person_gray);
            te_project.setTextColor(getResources().getColor(R.color.blue));
            te_activity.setTextColor(getResources().getColor(R.color.gray));
            te_honor.setTextColor(getResources().getColor(R.color.gray));
            te_person.setTextColor(getResources().getColor(R.color.gray));
        }else if(current == 1){
            img_project.setImageResource(R.drawable.project_gray);
            img_activity.setImageResource(R.drawable.activity_blue);
            img_honor.setImageResource(R.drawable.honor_gray);
            img_person.setImageResource(R.drawable.person_gray);
            te_project.setTextColor(getResources().getColor(R.color.gray));
            te_activity.setTextColor(getResources().getColor(R.color.blue));
            te_honor.setTextColor(getResources().getColor(R.color.gray));
            te_person.setTextColor(getResources().getColor(R.color.gray));
        }else if(current == 2){
            img_project.setImageResource(R.drawable.project_gray);
            img_activity.setImageResource(R.drawable.activity_gray);
            img_honor.setImageResource(R.drawable.honor_blue);
            img_person.setImageResource(R.drawable.person_gray);
            te_project.setTextColor(getResources().getColor(R.color.gray));
            te_activity.setTextColor(getResources().getColor(R.color.gray));
            te_honor.setTextColor(getResources().getColor(R.color.blue));
            te_person.setTextColor(getResources().getColor(R.color.gray));
        }else if(current == 3){
            img_project.setImageResource(R.drawable.project_gray);
            img_activity.setImageResource(R.drawable.activity_gray);
            img_honor.setImageResource(R.drawable.honor_gray);
            img_person.setImageResource(R.drawable.person_blue);
            te_project.setTextColor(getResources().getColor(R.color.gray));
            te_activity.setTextColor(getResources().getColor(R.color.gray));
            te_honor.setTextColor(getResources().getColor(R.color.gray));
            te_person.setTextColor(getResources().getColor(R.color.blue));
        }

    }

    //添加Frament
    public void addFrament(){
        mFramentList = new ArrayList<>();
        mainActFrament = new MainActFrament();
        mainHonFrament = new MainHonFrament();
        mainPerFrament = new MainPerFrament();
        mainProFrament = new MainProFrament();
        mFramentList.add(mainProFrament);
        mFramentList.add(mainActFrament);
        mFramentList.add(mainHonFrament);
        mFramentList.add(mainPerFrament);
    }

    //手动设置ViewPager要显示的视图
    private void changeView(int desTab)
    {
        viewPager.setCurrentItem(desTab, true);
    }

    //获取用户类型
    public String getUserType(){
        return "2";
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_pro_rela:
                changeView(0);
                changeColor(0);
                break;
            case R.id.main_act_rela:
                changeView(1);
                changeColor(1);
                break;
            case R.id.main_hon_rela:
                changeView(2);
                changeColor(2);
                break;
            case R.id.main_per_rela:
                changeView(3);
                changeColor(3);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if ( position == 0){
            changeColor(0);
        }else if(position == 1){
            changeColor(1);
        }else if(position == 2){
            changeColor(2);
        }else if(position == 3){
            changeColor(3);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
